<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--
	Created by util on ${now?date}.
-->
<#assign x = 0>
<mapper namespace="${package}.dao.${model}Dao">
    <resultMap id="${model?uncap_first}" type="${package}.model.${model}">
    <#list fields as f>
        <#if f.key??&&f.key=='PRI'>
        	<#assign x = 1>
            <id property="${f.field}" column="${f.field_}"/>
        <#else>
            <result property="${f.field}" column="${f.field_}"/>
        </#if>
    </#list>
    </resultMap>

    <sql id="columns">
    <#list fields as f>
        <#if f_index==0>
            ${f.field_} ,<#rt/>
        <#elseif f_has_next>
            <#t/>${f.field_} ,<#rt/>
        <#else >
            <#lt/>${f.field_}
        </#if>
    </#list>
    </sql>

    <insert id="insert${model}" <#if x=1>useGeneratedKeys="true" keyProperty="id"</#if>>
        insert into ${table}
        <trim prefix="(" suffix=")" suffixOverrides=",">
        <#list fields as f>
            <if test="${f.field} != null">
                ${f.field_},
            </if>
        </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
        <#list fields as f>
            <if test="${f.field} != null">
            ${r'#{'}${f.field}${r'}'} ,
            </if>
        </#list>
        </trim>
    </insert>

    <delete id="delete${model}">
        delete from ${table}
        <trim prefix="where " prefixOverrides="and ">
        <#list fields as f>
            <if test="${f.field} != null">
                and ${f.field_} = ${r'#{'}${f.field}${r'}'}
            </if>
        </#list>
        </trim>
    </delete>

    <update id="update${model}">
        update ${table}
        <set>
        <#list fields as f>
            <#if (f_index > 0)>
                <if test="${f.field} != null">
                    ${f.field_} = ${r'#{'}${f.field}${r'}'},
                </if>
            </#if>
        </#list>
        </set>
        where id = ${r'#{id}'}
    </update>

    <select id="select${model}ById" resultMap="${model?uncap_first}">
        select
        <include refid="columns" />
        from ${table}
        where id = ${r'#{id}'}
    </select>

    <select id="select${model}List" resultMap="${model?uncap_first}">
        select
        <include refid="columns" />
        from ${table}
        <trim prefix="where " prefixOverrides="and ">
        <#list fields as f>
            <if test="${f.field} != null">
                and ${f.field_} = ${r'#{'}${f.field}${r'}'}
            </if>
        </#list>
        </trim>
    </select>
    
    <select id="select${model}Page" resultMap="${model?uncap_first}">
        select
        <include refid="columns" />
        from ${table}
        <trim prefix="where " prefixOverrides="and ">
        <#list fields as f>
            <if test="${model?uncap_first}.${f.field} != null">
                and ${f.field_} = ${r'#{'}${model?uncap_first}.${f.field}${r'}'}
            </if>
        </#list>
        </trim>
    </select>
</mapper>